<?php
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// Get the request data
$data = json_decode(file_get_contents("php://input"));

if (!empty($data->name)) {
    $searchName = $data->name;

    // Connect to the database
    $servername = "localhost";
    $username = "root";
    $password = "root";
    $dbname = "fish";

    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Set character set
    $conn->set_charset("utf8");

    // Perform the search query
    $sql = "SELECT * FROM vuemodel WHERE name LIKE ?";
    $stmt = $conn->prepare($sql);
    $searchTerm = "%" . $searchName . "%";
    $stmt->bind_param("s", $searchTerm);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $vuemodel = array();
        while ($row = $result->fetch_assoc()) {
            array_push($vuemodel, $row);
        }
        echo json_encode(array("status" => 200, "data" => $vuemodel));
    } else {
        echo json_encode(array("status" => 404, "message" => "没有搜索到内容."));
    }

    $stmt->close();
    $conn->close();
} else {
    echo json_encode(array("status" => 400, "message" => "Search term is required."));
}
?>
